import router from '@/router/index'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getAccessToken } from '@/utils/auth'
import { useUserStoreWithOut } from '@/store/modules/user'

const whiteList = ['/login']

router.beforeEach(async (to, _from, next) => {
  NProgress.start()

  const userStore = useUserStoreWithOut()

  if (getAccessToken()) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      // 判断当前用户是否已拉取完登录信息
      if (userStore.userInfoIsEmpty()) {
        await userStore.setUserInfo()
      }

      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
